<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="手机号码" prop="phone" style="width:100%">
            <el-input v-model.number="ruleForm.phone" id="butinput" placeholder="请输入登录号码"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width:100%">
            <el-input v-model.number="ruleForm.password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="item" style="text-align: center">
            <el-button style="width: 40%;text-align: center" type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button style="width: 40%;text-align: center" @click="regist('first')">注册</el-button>
        </el-form-item>
    </el-form>
    <el-button :plain="true" @click="open2()" Style="display:none">成功</el-button>
    <el-button :plain="true" @click="open4()" Style="display:none">错误</el-button>
  </div>
</template>
<script>
import users from "@/api/users";
  export default {
    props:['msg'],
    data() {
      return {
        disabled:false,
        ruleForm: {
          phone: '',
          password:'',
        },
        rules: {
          phone: [
            { required: true, message: '手机号不能为空',trigger: 'blur'},
            {message: '手机号填写错误',trigger: 'blur',pattern:/^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/}
          ],
          password:[
              { required: true, message: '密码不能为空',trigger: 'blur'},
          ]
        },
      };
    },
    methods: {
      open2() {
        this.$message({
          message: '登录成功',
          type: 'success'
        });
      },
      open4(){
        this.$message.error("账号或密码错误！请重新输入！")
      },
      regist(data){
        this.$emit('registTo',data)
      },
      submitForm(formName) {
        var _this=this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            users.login(this.ruleForm.phone,this.ruleForm.password).then(function(response){
              if(response.data.data!=null){
                _this.open2();
                if (response.data.data.authority==4){
                  let user = response.data.data;
                  sessionStorage.setItem("admin",user.id);
                  _this.$router.push('/backstage');
                }
                let user = response.data.data;
                // console.log(user);
                // user.phone = user.phone.substring(0,3)+'****'+user.phone.substring(7,11)
                _this.resetSetItem("users",JSON.stringify(user));
                sessionStorage.setItem("uid",user.id);
                _this.$emit('close',false);
                _this.$emit('getU',1);
                //_this.$router.push('/main');
              }else{
                _this.open4();
              }
            })
          } else {
          console.log('error submit!!');
          return false;
          }
        });
      }
    },
  }
</script>
<style scoped>
#but{
   position: absolute;
   margin-left: 10px;
}
.item>>>.el-form-item__content{
    margin-left: 0 !important;
}
</style>